#ifndef UDPNODE_H
#define UDPNODE_H

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/errno.h>
#include <pthread.h>
#include <string>

#include "twindow.h"
#include "TransAnalysis.h"

#define SERV_PORT 5134

#define MAXNAME 1024
#define BUFSIZE 2048
extern int errno;

/**
 * UDP node implement a simple UPD connection node.
 * They communicate to other node, exchange Transactions.
 */
class UdpNode
{
public:
    UdpNode(int Port);
    ~UdpNode();
    int start();

    void sendUdpPack(const std::string& message);
    void setRemote(const std::string& IP, int Port);
    void setPacker(TransPack<TWindow>* Packer);

    void setUpStream(TWindow* Upstream) { upstream = Upstream; }
private:
    int port;
    int socket_udp;
    char buf[BUFSIZE];
    struct sockaddr_in myaddr;          /* address of this service */
    struct sockaddr_in client_addr;     /* address of client    */
    int sizeofClient;

    TransPack<TWindow>* pack;
    TWindow* upstream;

    pthread_t thread_recv;
    static void* receiveUdpPack(void*);
};

#endif // UDPNODE_H
